// pages/user/money.js
import { User } from '../../model/user.js';
var user = new User();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden: false,
    userInfo: {},
    loading: false,
    money: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    });
    this.getUserInfo();
  },
   /**
   * 获取用户信息
   */
  getUserInfo() {
    user.getUserInfo((result) => {
      this.setData({
        userInfo: result,
        hidden: true,
      })
      wx.hideLoading();
    })
  },
  openWin() {
    wx.navigateTo({
      url: 'income',
    })
  },
  createMoney() {
    const self = this;
    if (this.data.loading){
      return false
    }
    this.setData({
      loading: true
    })
    user.createMoney({ money: this.data.money }, (isVild, res)=>{
      if (isVild) {
        wx.showModal({
          title: '温馨提示',
          content: '提现成功',
          showCancel: false,
          confirmText: '知道了',
          confirmColor: '#F3474E',
          success: function (res) {
            if (res.confirm) {
              self.setData({
                loading: false
              })
              wx.showLoading({
                title: '加载中',
              });
              self.getUserInfo();
              let pages = getCurrentPages();   //获取小程序页面栈
              let beforePage = pages[pages.length - 2];  //获取上个页面的实例对象
              beforePage.getUserInfo();   //触发上个页面自定义的go_update方法
            }
          }
        })
      } else {
        this.setData({
          loading: false
        })
        for (var tmp in res.msg) {
          wx.showToast({
            title: res.msg[tmp],
            icon: 'none',
            duration: 2000
          })
          return false
        }
      }
    })
  },
  bindKeyInput: function (e) {
    this.setData({
      money: e.detail.value
    })
  },
})